<template>
  <div id="tags-view-container" class="tags-view-container sz pl10 pr10 pt10">
    <div class="tags-view-arrow poi g9" @click="goScroll('start')">
      <span class="el-icon-arrow-left"></span>
    </div>
    <scroll-pane ref="scrollPane" class="tags-view-wrapper ml10 mr10">
      <router-link
        v-for="(tag, index) in getVisitedViews"
        ref="tag"
        :key="tag.fullPath"
        :class="isActive(tag) ? 'active' : ''"
        :to="{ path: tag.path, query: tag.query }"
        tag="span"
        class="tags-view-item"
      >
        {{ tag.meta.title }}
        <span
          v-if="getVisitedViews.length > 1"
          class="el-icon-close"
          @click.prevent.stop="delTag(tag, index)"
        />
      </router-link>
    </scroll-pane>
    <div class="tags-view-arrow poi g9" @click="goScroll('end')">
      <span class="el-icon-arrow-right"></span>
    </div>
  </div>
</template>

<script>
import ScrollPane from "./ScrollPane";
import { mapGetters, mapActions } from "vuex";
export default {
  components: { ScrollPane },
  computed: {
    ...mapGetters(["getVisitedViews"]),
  },
  watch: {
    $route(v) {
      this.addVisitedView(v);
      this.moveToCurrentTag();
    },
  },
  mounted() {
    this.addVisitedView(this.$route);
  },
  methods: {
    ...mapActions(["addVisitedView", "delVisitedView"]),
    goScroll(key) {
      let tags = this.$refs.tag;
      if (tags.length > 1) {
        if (key === "start") {
          this.$refs.scrollPane.moveToTarget(tags[0]);
        } else {
          this.$refs.scrollPane.moveToTarget(tags[tags.length - 1]);
        }
      }
    },
    isActive(route) {
      return route.fullPath === this.$route.fullPath;
    },
    delTag(tag, index) {
      this.delVisitedView(index);
      if (this.isActive(tag)) {
        this.toLastView();
      }
    },
    toLastView() {
      const latestView = this.getVisitedViews.slice(-1)[0];
      if (latestView) {
        this.$router.push(latestView.fullPath);
      }
    },
    moveToCurrentTag() {
      const tags = this.$refs.tag;
      this.$nextTick(() => {
        for (const tag of tags) {
          if (tag.to.path === this.$route.path) {
            this.$refs.scrollPane.moveToTarget(tag);
            break;
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.tags-view-container {
  display: flex;
  align-items: center;
  width: 100%;
  background: #f0f2f5;
  z-index: 99;
  border-bottom: 2px solid #e7e7e7;

  .tags-view-arrow {
    height: 20px;
    width: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #e7e7e7;
    background: #fff;
  }

  .tags-view-wrapper {
    flex: 1;
    margin-bottom: -2px;
    .tags-view-item {
      border-radius: 4px 4px 0 0;
      display: inline-block;
      position: relative;
      cursor: pointer;
      height: 34px;
      line-height: 34px;
      color: #495060;
      padding: 0 10px;
      font-size: 12px;
      background: #fafbfc;
      border-top: 2px solid #e7e7e7;
      border-left: 1px solid #e7e7e7;
      border-right: 1px solid #e7e7e7;
      border-bottom: 2px solid #e7e7e7;
      &:first-child {
        border-left: 2px solid #e7e7e7;
      }
      &:last-child {
        border-right: 2px solid #e7e7e7;
      }
      &.active {
        background-color: #fff;
        color: var(--hancode-color);
        border-top-color: var(--hancode-color);
        border-bottom-color: #fff;
        &::before {
          background: var(--hancode-color);
        }
      }
      &::before {
        content: "";
        background: #999;
        display: inline-block;
        width: 6px;
        height: 6px;
        border-radius: 50%;
        position: relative;
        margin-right: 5px;
        top: -1px;
      }
    }
  }
  .contextmenu {
    margin: 0;
    background: #fff;
    z-index: 3000;
    position: absolute;
    list-style-type: none;
    padding: 5px 0;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 400;
    color: #333;
    box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, 0.3);
    li {
      margin: 0;
      padding: 7px 16px;
      cursor: pointer;
      &:hover {
        background: #eee;
      }
    }
  }
}
</style>

<style lang="scss">
.tags-view-wrapper {
  .tags-view-item {
    .el-icon-close {
      width: 16px;
      height: 16px;
      vertical-align: 2px;
      border-radius: 50%;
      text-align: center;
      transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
      transform-origin: 100% 50%;
      &:before {
        transform: scale(0.6);
        display: inline-block;
        vertical-align: -3px;
      }
      &:hover {
        background-color: #b4bccc;
        color: #fff;
      }
    }
  }
}
</style>
